/*
 * MaskBuilder.java
 *
 * Created on August 6, 2004, 10:42 PM
 */

/**
 *
 * @author  pablo
 */

package edu.umd.essic.maskbuilder;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.image.renderable.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.ImageIcon;
import javax.swing.filechooser.*;
import java.io.File;
import java.awt.print.*;


public class MaskBuilder extends javax.swing.JFrame{

    static final String FUNC_SIMPLE = "1";
    static final String FUNC_ESSIC = "2";
    static final String FUNC_2D = "3";
    static private final String NEWLINE = "\n";
    static private final float ZOOM_MIN = 0.125f;
    private float ZOOM_MAX = 2.f;
    private float ZOOM_INIT = 1.0f;
    private float scale_factor = 0.5f;
    static int FixedWidth = 1024;
    private boolean add_layer = false; 
    private boolean palette = true; // colored
    JFileChooser fc;        //define dialog filechooser
    constGridDialog cgd;    //define dialog for regular grid definition
    MaskDepthDlg cdm;       //define dialog for reseting mask elevation threshold
    Dialog2D Dialog2d;
    ESSICDialog EssicDlg;
    FileContentsDlg FileDlg;
    MapFileDlg mfd;
    //Int_Dialog IDialog;
    ErrorDlg erd;
    Map map;                // define MAP OBJECT
    Grid grid;              // define GRID OBJECT
    Mask mask;              // define MASK OBJECT
    Contour contour;        // define Contour OBJECT
    IThread ithread;        // define Thread for ecodata interpolation
    FThread fthread;        // define Thread for fishery data interpolation
    File resultMask;        // define MASK output file variable
    ImageIcon imageicon;    // Picture for the screen
    Rectangle draggedrect;  // Define rectangle, that used for rectangular region selections in GUI
    float zoom_level;       // zoom level for view
    int mask_edit_flag;     // flag for editable layer
    // FLAGS SECTION flag name indicate purpose
    boolean map_exist;    
    boolean grid_exist;   boolean grid_shown;
    boolean mask_exist;   boolean mask_shown;
    boolean contour_exist;boolean contour_shown;
    boolean itopo_exist;  boolean itopo_shown;
    boolean patch_mode;
    boolean mask_editable;
    String MapFileName;
    String WorkingDir;
    Color[] mask_colors;
    int nb_layers;

    public MaskBuilder() {
        // init GUI
        Image img = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/edu/umd/essic/maskbuilder/world.jpg"));
        setIconImage(img);        
        initComponents();
        //FLAGS initialization
        grid_exist = false; grid_shown   = false;
        map_exist  = false; zoom_level   = ZOOM_INIT;
        mask_exist = false; mask_shown   = false; 
        contour_exist = false; contour_shown = false;
        itopo_exist = false;   itopo_shown = false;
        mask_editable = true;
        patch_mode = true;
        
        //DIALOGS section
        //grid constructing dialog
        cgd = new constGridDialog(this, true);
        cgd.setLocationRelativeTo(this);
        cgd.setLocation(20,20);
        
        //change elevation threshold for mask
        cdm = new MaskDepthDlg(this, true);
        cdm.setLocationRelativeTo(this);
        cdm.setLocation(100,30);
        
        EssicDlg = new ESSICDialog(this, true);
        EssicDlg.setLocationRelativeTo(this);
        EssicDlg.setLocation(20,20);
        
        Dialog2d = new Dialog2D(this,true);
        Dialog2d.setLocationRelativeTo(this);
        Dialog2d.setLocation(20,20); 
        
        mfd = new MapFileDlg(this,true);
        mfd.setLocationRelativeTo(this);
        mfd.setLocation(20,20);
        
        FileDlg = new FileContentsDlg(this,true);
        FileDlg.setLocationRelativeTo(this);
        FileDlg.setLocation(20,20);
           
        contour = null;
        CleanGridInfo();
        MenuChecker();
        //start with one layer
        nb_layers = 1;
        GenMaskColors();
    }
    

//*****************************************
//Menu Checker - disable and enable menu items
//    
private void MenuChecker(){
    if (!map_exist && ! grid_exist && ! mask_exist){
        ConsGridMenuItem.setEnabled(false);
        GridShownCheckBox.setEnabled(false);
        MaskShownCheckBox.setEnabled(false);
        StretchCombMenuItem.setEnabled(false);
        EssicStretchMenuItem.setEnabled(false);
        Stretch2DMenuItem.setEnabled(false);
        SaveAllMenuItem.setEnabled(false);
        SaveGridMenuItem.setEnabled(false);
        BuildMaskMenuItem.setEnabled(false);
        SaveMaskMenuItem.setEnabled(false);
        SaveTopoMenuItem.setEnabled(false);
        TopographyMenuItem.setEnabled(false);
        TopographyShownCheckBox.setEnabled(false);
        MaskModeCheckBox.setEnabled(false);
        ChooseRuleMenu.setEnabled(false);
        InterpolationItem.setEnabled(false);
        FisheryItem.setEnabled(false);
        restoreMenuItem.setEnabled(false);
        LoadContourMenuItem.setEnabled(false);
        LoadMaskMenuItem.setEnabled(false);
        ResetThresholdMenu.setEnabled(false);
        ButtonGrid.setEnabled(false);
        ButtonMaskBuild.setEnabled(false);
        PrintAllMenuItem.setEnabled(false);
        ButtonPrint.setEnabled(false);
        ZoomInMenuItem.setEnabled(false);
        ZoomOutMenuItem.setEnabled(false);
        EditMaskButton.setEnabled(false);
        ContourShownCheckBox.setEnabled(false);
        FilterMenuItem.setEnabled(false);
    }
    if (mask_exist){
        SaveMaskMenuItem.setEnabled(true);
        TopographyMenuItem.setEnabled(true);
        if (itopo_exist){
            SaveTopoMenuItem.setEnabled(true);
            TopographyShownCheckBox.setEnabled(true);
        }
        MaskModeCheckBox.setEnabled(true);
        ChooseRuleMenu.setEnabled(true);
        InterpolationItem.setEnabled(true);
        FisheryItem.setEnabled(true);
        TButtonMask.setEnabled(true);
        SaveAllMenuItem.setEnabled(true);
        MaskShownCheckBox.setEnabled(true);
        FilterMenuItem.setEnabled(true);
        EditMaskButton.setEnabled(true);
        mask_shown = MaskShownCheckBox.getState();
        grid_shown = GridShownCheckBox.getState();
        mask_editable = EditMaskButton.isSelected();
    } else if (grid_exist){
        GridShownCheckBox.setEnabled(true);
        EssicStretchMenuItem.setEnabled(true);
        Stretch2DMenuItem.setEnabled(true);
        StretchCombMenuItem.setEnabled(true);
        SaveGridMenuItem.setEnabled(true);
        TButtonScale.setEnabled(true);
        ButtonMaskBuild.setEnabled(true);
        BuildMaskMenuItem.setEnabled(true);
        ResetThresholdMenu.setEnabled(true);   
        LoadMaskMenuItem.setEnabled(true);
        TButtonMask.setSelected(patch_mode);
        grid_shown = GridShownCheckBox.getState();
    } else if (map_exist){
        ConsGridMenuItem.setEnabled(true);
        restoreMenuItem.setEnabled(true);
        LoadContourMenuItem.setEnabled(true);
        ButtonGrid.setEnabled(true);
        PrintAllMenuItem.setEnabled(true);
        ButtonPrint.setEnabled(true);
        ButtonZoomIn.setEnabled(true);
        ButtonZoomOut.setEnabled(true);
        ZoomInMenuItem.setEnabled(true);
        ZoomOutMenuItem.setEnabled(true);
    }
    if (contour_exist){
        ContourShownCheckBox.setEnabled(true);
        contour_shown = ContourShownCheckBox.isSelected();
    }
}
    
private void RefreshDisplay(){
    MenuChecker();
    GenMaskColors();
    BufferedImage displayimage;
    displayimage = null;
    if(map_exist){
      if (zoom_level != 1){
          int w = Math.round(map.GetWidth()*zoom_level);
          int h = Math.round(map.GetHeight()*zoom_level);
          BufferedImage bi = map.ReturnImage(true);
          Image si = bi.getScaledInstance(w, h, Image.SCALE_FAST);
          displayimage = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
          Graphics2D g = displayimage.createGraphics();
          g.drawImage(si, 0, 0, null);
          g.dispose();
      }else{
          displayimage = map.ReturnImage(true);
      }
      Graphics2D drawing = displayimage.createGraphics();
      if (grid_exist){
            int size_x = grid.Get_NX();
            int size_y = grid.Get_NY();
            Point[][] coord = grid.Get_Im_Coords(zoom_level);
            if (mask_exist){
                int[][] msk = mask.GetMask();
                if (mask_shown){
                    if (!patch_mode){
                        Point[][] crd = mask.GetNodes(zoom_level);
                        map.Show_Unpatched_Mask(drawing,msk,crd,mask.GetLonDim(),mask.GetLatDim(),mask.GetFlag(),mask_colors);
                    } else
                        map.Show_Patched_Mask(drawing,msk,coord,mask.GetLonDim(),mask.GetLatDim(),mask.GetFlag(),mask_colors);
                } 
                if (itopo_exist && itopo_shown)
                    map.Show_Topo_Indices(drawing,msk,coord,mask.GetLonDim(),mask.GetLatDim());
            }
            if (grid_shown){
                drawing.setColor(Color.BLACK);
                for(int i=0; i<size_x-1; i++){
                    for(int j=0;j<size_y-1;j++){
                        Polygon p = new Polygon();
                        p.addPoint(coord[i][j].x,coord[i][j].y);
                        p.addPoint(coord[i+1][j].x,coord[i+1][j].y);
                        p.addPoint(coord[i+1][j+1].x,coord[i+1][j+1].y);
                        p.addPoint(coord[i][j+1].x,coord[i][j+1].y);
                        drawing.drawPolygon(p);
                        p = null;
                    }
                }
            }
      }
      if(contour_exist && contour_shown){contour.Draw(displayimage, zoom_level);}
      if (map_exist){ 
          imageicon.setImage(new ImageIcon(displayimage).getImage());
          ImageLabel.setIcon(imageicon);
      }
      MyScrollPane.getViewport().removeAll();
      MyScrollPane.getViewport().add(ImageLabel);
      drawing.dispose();
    }   
    System.gc();
}        

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        MyScrollPane = new javax.swing.JScrollPane();
        ImageLabel = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        ButtonMapLoad = new javax.swing.JButton();
        ButtonGrid = new javax.swing.JButton();
        ButtonMaskBuild = new javax.swing.JButton();
        jSeparator5 = new javax.swing.JSeparator();
        TButtonScale = new javax.swing.JToggleButton();
        TButtonMask = new javax.swing.JToggleButton();
        EditMaskButton = new javax.swing.JToggleButton();
        jSeparator6 = new javax.swing.JSeparator();
        ButtonZoomIn = new javax.swing.JButton();
        ButtonZoomOut = new javax.swing.JButton();
        ButtonPrint = new javax.swing.JButton();
        HintPanel = new javax.swing.JPanel();
        CoordinateField = new javax.swing.JTextField();
        InfoField = new javax.swing.JTextField();
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        LoadMapMenuItem = new javax.swing.JMenuItem();
        LoadSavedDataMenuItem = new javax.swing.JMenuItem();
        LoadMaskMenuItem = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        SaveGridMenuItem = new javax.swing.JMenuItem();
        SaveMaskMenuItem = new javax.swing.JMenuItem();
        SaveTopoMenuItem = new javax.swing.JMenuItem();
        SaveAllMenuItem = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JSeparator();
        PrintAllMenuItem = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JSeparator();
        exitMenuItem = new javax.swing.JMenuItem();
        mapMenu = new javax.swing.JMenu();
        LoadContourMenuItem = new javax.swing.JMenuItem();
        ContourShownCheckBox = new javax.swing.JCheckBoxMenuItem();
        jSeparator8 = new javax.swing.JSeparator();
        TopographyMenuItem = new javax.swing.JMenuItem();
        TopographyShownCheckBox = new javax.swing.JCheckBoxMenuItem();
        gridMenu = new javax.swing.JMenu();
        ConsGridMenuItem = new javax.swing.JMenuItem();
        GridShownCheckBox = new javax.swing.JCheckBoxMenuItem();
        EssicStretchMenuItem = new javax.swing.JMenuItem();
        Stretch2DMenuItem = new javax.swing.JMenuItem();
        StretchCombMenuItem = new javax.swing.JMenuItem();
        maskMenu = new javax.swing.JMenu();
        ResetThresholdMenu = new javax.swing.JMenuItem();
        BuildMaskMenuItem = new javax.swing.JMenuItem();
        FilterMenuItem = new javax.swing.JMenuItem();
        MaskShownCheckBox = new javax.swing.JCheckBoxMenuItem();
        jSeparator4 = new javax.swing.JSeparator();
        MaskModeCheckBox = new javax.swing.JCheckBoxMenuItem();
        ChooseRuleMenu = new javax.swing.JMenu();
        XOR_RadioButton = new javax.swing.JRadioButtonMenuItem();
        TRUE_RadioButton = new javax.swing.JRadioButtonMenuItem();
        FALSE_RadioButton = new javax.swing.JRadioButtonMenuItem();
        AnyLayerMenuItem = new javax.swing.JMenuItem();
        DataMenu = new javax.swing.JMenu();
        InterpolationItem = new javax.swing.JMenuItem();
        FisheryItem = new javax.swing.JMenuItem();
        ViewMenu = new javax.swing.JMenu();
        PaletteMenu = new javax.swing.JMenu();
        BWCheckBoxMenuItem = new javax.swing.JCheckBoxMenuItem();
        ColCheckBoxMenuItem = new javax.swing.JCheckBoxMenuItem();
        jSeparator7 = new javax.swing.JSeparator();
        ZoomFactorMenuItem = new javax.swing.JMenuItem();
        ZoomInMenuItem = new javax.swing.JMenuItem();
        ZoomOutMenuItem = new javax.swing.JMenuItem();
        restoreMenuItem = new javax.swing.JMenuItem();
        helpMenu = new javax.swing.JMenu();
        contentsMenuItem = new javax.swing.JMenuItem();
        aboutMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("GMB 2.2");

        MyScrollPane.setBorder(null);
        MyScrollPane.setPreferredSize(new java.awt.Dimension(800, 600));
        MyScrollPane.setOpaque(false);

        ImageLabel.setBackground(new java.awt.Color(255, 255, 255));
        ImageLabel.setFont(new java.awt.Font("MS Sans Serif", 1, 13));
        ImageLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        ImageLabel.setText("No map loaded");
        ImageLabel.setMaximumSize(new java.awt.Dimension(32767, 32767));
        ImageLabel.setDebugGraphicsOptions(javax.swing.DebugGraphics.NONE_OPTION);
        ImageLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        ImageLabel.setOpaque(true);
        ImageLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ImageLabelMouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                ImageLabelMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                ImageLabelMouseReleased(evt);
            }
        });
        ImageLabel.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                ImageLabelMouseDragged(evt);
            }
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                ImageLabelMouseMoved(evt);
            }
        });
        MyScrollPane.setViewportView(ImageLabel);

        getContentPane().add(MyScrollPane, java.awt.BorderLayout.CENTER);

        jPanel1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        ButtonMapLoad.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/umd/essic/maskbuilder/MapLoad.gif"))); // NOI18N
        ButtonMapLoad.setToolTipText("Load map");
        ButtonMapLoad.setBorder(null);
        ButtonMapLoad.setName("Load map"); // NOI18N
        ButtonMapLoad.setPreferredSize(new java.awt.Dimension(24, 24));
        ButtonMapLoad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LoadMapMenuItemActionPerformed(evt);
            }
        });
        jPanel1.add(ButtonMapLoad);

        ButtonGrid.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/umd/essic/maskbuilder/Grid.gif"))); // NOI18N
        ButtonGrid.setToolTipText("Define regular grid");
        ButtonGrid.setBorder(null);
        ButtonGrid.setPreferredSize(new java.awt.Dimension(24, 24));
        ButtonGrid.setEnabled(false);
        ButtonGrid.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ConsGridMenuItemActionPerformed(evt);
            }
        });
        jPanel1.add(ButtonGrid);

        ButtonMaskBuild.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/umd/essic/maskbuilder/Mask.gif"))); // NOI18N
        ButtonMaskBuild.setToolTipText("Build mask");
        ButtonMaskBuild.setBorder(null);
        ButtonMaskBuild.setPreferredSize(new java.awt.Dimension(24, 24));
        ButtonMaskBuild.setEnabled(false);
        ButtonMaskBuild.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BuildMaskMenuItemActionPerformed(evt);
            }
        });
        jPanel1.add(ButtonMaskBuild);

        jSeparator5.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jSeparator5.setPreferredSize(new java.awt.Dimension(2, 18));
        jSeparator5.setDebugGraphicsOptions(javax.swing.DebugGraphics.NONE_OPTION);
        jSeparator5.setEnabled(false);
        jPanel1.add(jSeparator5);

        TButtonScale.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/umd/essic/maskbuilder/Scaling.gif"))); // NOI18N
        TButtonScale.setToolTipText("Scale map and grid");
        TButtonScale.setBorder(javax.swing.BorderFactory.createCompoundBorder());
        TButtonScale.setMinimumSize(new java.awt.Dimension(24, 24));
        TButtonScale.setPreferredSize(new java.awt.Dimension(24, 24));
        TButtonScale.setEnabled(false);
        TButtonScale.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TButtonScaleActionPerformed(evt);
            }
        });
        jPanel1.add(TButtonScale);

        TButtonMask.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/umd/essic/maskbuilder/MapEdit.gif"))); // NOI18N
        TButtonMask.setToolTipText("Show mask in patch mode");
        TButtonMask.setBorderPainted(false);
        TButtonMask.setMinimumSize(new java.awt.Dimension(24, 24));
        TButtonMask.setPreferredSize(new java.awt.Dimension(24, 24));
        TButtonMask.setEnabled(false);
        TButtonMask.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TButtonMaskActionPerformed(evt);
            }
        });
        jPanel1.add(TButtonMask);

        EditMaskButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/umd/essic/maskbuilder/maskEdit.gif"))); // NOI18N
        EditMaskButton.setToolTipText("Enable mask edit");
        EditMaskButton.setBorderPainted(false);
        EditMaskButton.setMinimumSize(new java.awt.Dimension(24, 24));
        EditMaskButton.setPreferredSize(new java.awt.Dimension(24, 24));
        EditMaskButton.setEnabled(false);
        EditMaskButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EditMaskButtonActionPerformed(evt);
            }
        });
        jPanel1.add(EditMaskButton);

        jSeparator6.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jSeparator6.setPreferredSize(new java.awt.Dimension(2, 18));
        jSeparator6.setDebugGraphicsOptions(javax.swing.DebugGraphics.NONE_OPTION);
        jSeparator6.setEnabled(false);
        jPanel1.add(jSeparator6);

        ButtonZoomIn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/umd/essic/maskbuilder/Zoomin.gif"))); // NOI18N
        ButtonZoomIn.setToolTipText("Zoom in");
        ButtonZoomIn.setBorder(javax.swing.BorderFactory.createCompoundBorder());
        ButtonZoomIn.setPreferredSize(new java.awt.Dimension(24, 24));
        ButtonZoomIn.setEnabled(false);
        ButtonZoomIn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ZoomInMenuItemActionPerformed(evt);
            }
        });
        jPanel1.add(ButtonZoomIn);

        ButtonZoomOut.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/umd/essic/maskbuilder/Zoomout.gif"))); // NOI18N
        ButtonZoomOut.setToolTipText("Zoom out");
        ButtonZoomOut.setBorder(javax.swing.BorderFactory.createCompoundBorder());
        ButtonZoomOut.setPreferredSize(new java.awt.Dimension(24, 24));
        ButtonZoomOut.setEnabled(false);
        ButtonZoomOut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ZoomOutMenuItemActionPerformed(evt);
            }
        });
        jPanel1.add(ButtonZoomOut);

        ButtonPrint.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/umd/essic/maskbuilder/print.gif"))); // NOI18N
        ButtonPrint.setToolTipText("Print graphics");
        ButtonPrint.setBorder(javax.swing.BorderFactory.createCompoundBorder());
        ButtonPrint.setMaximumSize(new java.awt.Dimension(24, 24));
        ButtonPrint.setPreferredSize(new java.awt.Dimension(24, 24));
        ButtonPrint.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PrintAllMenuItemActionPerformed(evt);
            }
        });
        jPanel1.add(ButtonPrint);

        getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH);

        HintPanel.setMinimumSize(new java.awt.Dimension(4, 20));
        HintPanel.setPreferredSize(new java.awt.Dimension(0, 25));
        HintPanel.setLayout(new javax.swing.BoxLayout(HintPanel, javax.swing.BoxLayout.LINE_AXIS));

        CoordinateField.setEditable(false);
        CoordinateField.setFont(new java.awt.Font("MS Sans Serif", 1, 13));
        CoordinateField.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        CoordinateField.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        CoordinateField.setMaximumSize(new java.awt.Dimension(250, 25));
        CoordinateField.setMinimumSize(new java.awt.Dimension(4, 25));
        CoordinateField.setPreferredSize(new java.awt.Dimension(180, 25));
        HintPanel.add(CoordinateField);

        InfoField.setEditable(false);
        InfoField.setForeground(new java.awt.Color(153, 0, 51));
        InfoField.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        InfoField.setPreferredSize(new java.awt.Dimension(200, 25));
        HintPanel.add(InfoField);

        getContentPane().add(HintPanel, java.awt.BorderLayout.SOUTH);

        menuBar.setDoubleBuffered(true);

        fileMenu.setText("File");

        LoadMapMenuItem.setText("Load map...");
        LoadMapMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LoadMapMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(LoadMapMenuItem);

        LoadSavedDataMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.CTRL_MASK));
        LoadSavedDataMenuItem.setText("Load GMB data file...");
        LoadSavedDataMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LoadSavedDataMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(LoadSavedDataMenuItem);

        LoadMaskMenuItem.setText("Load land mask...");
        LoadMaskMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LoadMaskMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(LoadMaskMenuItem);
        fileMenu.add(jSeparator1);

        SaveGridMenuItem.setText("Save Seapodym grid...");
        SaveGridMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SaveGridMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(SaveGridMenuItem);

        SaveMaskMenuItem.setText("Save Seapodym mask...");
        SaveMaskMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SaveMaskMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(SaveMaskMenuItem);

        SaveTopoMenuItem.setText("Save Topographic Index...");
        SaveTopoMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SaveTopoMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(SaveTopoMenuItem);

        SaveAllMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F2, java.awt.event.InputEvent.CTRL_MASK));
        SaveAllMenuItem.setText("Save GMB data file...");
        SaveAllMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SaveAllMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(SaveAllMenuItem);
        fileMenu.add(jSeparator2);

        PrintAllMenuItem.setText("Print...");
        PrintAllMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PrintAllMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(PrintAllMenuItem);
        fileMenu.add(jSeparator3);

        exitMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.ALT_MASK));
        exitMenuItem.setText("Exit");
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        mapMenu.setText("Map");

        LoadContourMenuItem.setText("Load coastline...");
        LoadContourMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LoadContourMenuItemActionPerformed(evt);
            }
        });
        mapMenu.add(LoadContourMenuItem);

        ContourShownCheckBox.setSelected(true);
        ContourShownCheckBox.setText("Show coastline...");
        ContourShownCheckBox.setEnabled(false);
        ContourShownCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ContourShownCheckBoxActionPerformed(evt);
            }
        });
        mapMenu.add(ContourShownCheckBox);
        mapMenu.add(jSeparator8);

        TopographyMenuItem.setText("Topography Indices...");
        TopographyMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TopographyMenuItemActionPerformed(evt);
            }
        });
        mapMenu.add(TopographyMenuItem);

        TopographyShownCheckBox.setSelected(true);
        TopographyShownCheckBox.setText("Show topo-indices...");
        TopographyShownCheckBox.setEnabled(false);
        TopographyShownCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TopographyShownCheckBoxActionPerformed(evt);
            }
        });
        mapMenu.add(TopographyShownCheckBox);

        menuBar.add(mapMenu);

        gridMenu.setText("Grid");

        ConsGridMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_G, java.awt.event.InputEvent.CTRL_MASK));
        ConsGridMenuItem.setText("Define...");
        ConsGridMenuItem.setActionCommand("defConstGrid");
        ConsGridMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ConsGridMenuItemActionPerformed(evt);
            }
        });
        gridMenu.add(ConsGridMenuItem);

        GridShownCheckBox.setSelected(true);
        GridShownCheckBox.setText("Show Grid...");
        GridShownCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                GridShownCheckBoxActionPerformed(evt);
            }
        });
        gridMenu.add(GridShownCheckBox);

        EssicStretchMenuItem.setText("1D Stretching");
        EssicStretchMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EssicStretchMenuItemActionPerformed(evt);
            }
        });
        gridMenu.add(EssicStretchMenuItem);

        Stretch2DMenuItem.setText("2D Transform");
        Stretch2DMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Stretch2DMenuItemActionPerformed(evt);
            }
        });
        gridMenu.add(Stretch2DMenuItem);

        StretchCombMenuItem.setText("Combined Stretching");
        StretchCombMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                StretchCombMenuItemActionPerformed(evt);
            }
        });
        gridMenu.add(StretchCombMenuItem);

        menuBar.add(gridMenu);

        maskMenu.setText("Mask");

        ResetThresholdMenu.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_R, java.awt.event.InputEvent.CTRL_MASK));
        ResetThresholdMenu.setText("Mask properties...");
        ResetThresholdMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ResetThresholdMenuActionPerformed(evt);
            }
        });
        maskMenu.add(ResetThresholdMenu);

        BuildMaskMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_M, java.awt.event.InputEvent.CTRL_MASK));
        BuildMaskMenuItem.setText("Build Mask...");
        BuildMaskMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BuildMaskMenuItemActionPerformed(evt);
            }
        });
        maskMenu.add(BuildMaskMenuItem);

        FilterMenuItem.setText("Apply boundary filter...");
        FilterMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FilterMenuItemActionPerformed(evt);
            }
        });
        maskMenu.add(FilterMenuItem);

        MaskShownCheckBox.setSelected(true);
        MaskShownCheckBox.setText("Show Mask...");
        MaskShownCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MaskShownCheckBoxActionPerformed(evt);
            }
        });
        maskMenu.add(MaskShownCheckBox);
        maskMenu.add(jSeparator4);

        MaskModeCheckBox.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.CTRL_MASK));
        MaskModeCheckBox.setSelected(true);
        MaskModeCheckBox.setText("Patch Mode...");
        MaskModeCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MaskModeCheckBoxActionPerformed(evt);
            }
        });
        maskMenu.add(MaskModeCheckBox);

        ChooseRuleMenu.setText("Edit Mode");

        XOR_RadioButton.setSelected(true);
        XOR_RadioButton.setText("REVERSE Mode");
        XOR_RadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                XOR_RadioButtonActionPerformed(evt);
            }
        });
        ChooseRuleMenu.add(XOR_RadioButton);

        TRUE_RadioButton.setLabel("Set UPPER Layer");
        TRUE_RadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TRUE_RadioButtonActionPerformed(evt);
            }
        });
        ChooseRuleMenu.add(TRUE_RadioButton);

        FALSE_RadioButton.setLabel("Set LOWER Layer");
        FALSE_RadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FALSE_RadioButtonActionPerformed(evt);
            }
        });
        ChooseRuleMenu.add(FALSE_RadioButton);

        AnyLayerMenuItem.setText("Enter Layer Flag...");
        AnyLayerMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AnyLayerMenuItemActionPerformed(evt);
            }
        });
        ChooseRuleMenu.add(AnyLayerMenuItem);

        maskMenu.add(ChooseRuleMenu);

        menuBar.add(maskMenu);

        DataMenu.setText("Data");

        InterpolationItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_I, java.awt.event.InputEvent.CTRL_MASK));
        InterpolationItem.setText("Interpolate ecodata...");
        InterpolationItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                InterpolationItemActionPerformed(evt);
            }
        });
        DataMenu.add(InterpolationItem);

        FisheryItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F, java.awt.event.InputEvent.CTRL_MASK));
        FisheryItem.setText("Redistribute fishery...");
        FisheryItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FisheryItemActionPerformed(evt);
            }
        });
        DataMenu.add(FisheryItem);

        menuBar.add(DataMenu);

        ViewMenu.setText("View");

        PaletteMenu.setText("Palette");

        BWCheckBoxMenuItem.setText("Greyscale");
        BWCheckBoxMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BWCheckBoxMenuItemActionPerformed(evt);
            }
        });
        PaletteMenu.add(BWCheckBoxMenuItem);

        ColCheckBoxMenuItem.setSelected(true);
        ColCheckBoxMenuItem.setText("Colored");
        ColCheckBoxMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ColCheckBoxMenuItemActionPerformed(evt);
            }
        });
        PaletteMenu.add(ColCheckBoxMenuItem);

        ViewMenu.add(PaletteMenu);
        ViewMenu.add(jSeparator7);

        ZoomFactorMenuItem.setText("Zoom factor...");
        ZoomFactorMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ZoomFactorMenuItemActionPerformed(evt);
            }
        });
        ViewMenu.add(ZoomFactorMenuItem);

        ZoomInMenuItem.setText("Zoom In...");
        ZoomInMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ZoomInMenuItemActionPerformed(evt);
            }
        });
        ViewMenu.add(ZoomInMenuItem);

        ZoomOutMenuItem.setText("Zoom Out...");
        ZoomOutMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ZoomOutMenuItemActionPerformed(evt);
            }
        });
        ViewMenu.add(ZoomOutMenuItem);

        restoreMenuItem.setText("Original");
        restoreMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                restoreMenuItemActionPerformed(evt);
            }
        });
        ViewMenu.add(restoreMenuItem);

        menuBar.add(ViewMenu);

        helpMenu.setText("Help");

        contentsMenuItem.setText("Contents");
        contentsMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                contentsMenuItemActionPerformed(evt);
            }
        });
        helpMenu.add(contentsMenuItem);

        aboutMenuItem.setText("About");
        aboutMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutMenuItemActionPerformed(evt);
            }
        });
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setJMenuBar(menuBar);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void TopographyShownCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TopographyShownCheckBoxActionPerformed
        itopo_shown = TopographyShownCheckBox.isSelected();
        mask_shown = !TopographyShownCheckBox.isSelected();
        MaskShownCheckBox.setSelected(mask_shown);
        RefreshDisplay();
    }//GEN-LAST:event_TopographyShownCheckBoxActionPerformed

    private void TopographyMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TopographyMenuItemActionPerformed
        if(!mask_exist){
            JOptionPane.showMessageDialog(this,"Please, build grid and mask first!","Warning",JOptionPane.WARNING_MESSAGE);
            return;
        } else {  
            int mindepth = 100;//in meters
            int value; float coef;
            float slope = 0.01f;
            try {
                String s = JOptionPane.showInputDialog(this,"Enter depth (in meters) and sensitivity, " +
                           "\n" + "separated by comma (e.g. 100,0.1)","Topography index settings",JOptionPane.DEFAULT_OPTION);
                int to = s.indexOf(",");
                value = Integer.parseInt(s.substring(0,to));
                coef  = Float.parseFloat(s.substring(to+1));
            } catch (NumberFormatException e){return;}
            if ((value>0) && (value<2e4)) mindepth = value;
            if ((coef>0.001f) && (coef<1e3)) slope = coef;
            int size_x = grid.Get_NX();
            int size_y = grid.Get_NY();
            if (!cdm.getMaskType()){
                  size_x += 1;
                  size_y += 1;
             }              
            for(int j=0;j<size_y-1; j++){
                for(int i=0;i<size_x-1;i++){
                    int val = mask.CheckNode(i,j);
                    float I = 0f;
                    if (val>0){
                        Polygon p = grid.GetPolygon(i,j);
                        Rectangle r = p.getBounds();
                        I = map.Get_TopoIndex(r,mindepth,slope,i,j,size_x,size_y);
                    }
                }
            }
            itopo_shown = true;
            mask_shown = false;
            TopographyShownCheckBox.setEnabled(true);
            itopo_exist = true;
            RefreshDisplay();
        }
    }//GEN-LAST:event_TopographyMenuItemActionPerformed

    private void SaveTopoMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveTopoMenuItemActionPerformed
        if (itopo_exist){
            fc = new JFileChooser(".//output");
            fc.setAcceptAllFileFilterUsed(false);
            fc.addChoosableFileFilter(new FilesFilter(2));
            int returnVal = fc.showDialog(this,"Save topo-index");
            if (returnVal == JFileChooser.APPROVE_OPTION) {
            try{ 
                  File file = fc.getSelectedFile();
                  FileWriter out = new FileWriter(file);
                  int size_x = grid.Get_NX();
                  int size_y = grid.Get_NY();
                  if (!cdm.getMaskType()){
                      size_x += 1;
                      size_y += 1;
                  }    
                  float[][] I = map.GetTopoIndices();
                  for(int j=0;j<size_y-1; j++){
                    for(int i=0;i<size_x-1;i++){
                        String str = String.valueOf(I[i][j]); 
                        out.write(str);
                        out.write("\t");
                    }
                  out.write(NEWLINE);
                  }
                   out.close();
               }catch (IOException e) {
                System.out.println("Failed task...");    
                System.exit(0);}
                
            }
            fc = null;
        } 
    }//GEN-LAST:event_SaveTopoMenuItemActionPerformed

    private void ZoomFactorMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ZoomFactorMenuItemActionPerformed
        float value = scale_factor;
        try {
            String s = JOptionPane.showInputDialog(this,"Enter scaling factor (between 0 to 1)","Zoom",JOptionPane.DEFAULT_OPTION);
            if (s != null)
                value = Float.parseFloat(s);
        } catch (NumberFormatException e){return;}
        if (!(value>1)&&!(value<=0)) scale_factor = value;
    }//GEN-LAST:event_ZoomFactorMenuItemActionPerformed

    private void contentsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_contentsMenuItemActionPerformed
        String app;
        try{
            Runtime r = Runtime.getRuntime();
            String myOs  = System.getProperty("os.name"); 
            String myDir = System.getProperty("user.dir"); 
            //System.getProperties().list(System.out);
            if (myOs.startsWith("Windows")){
                app = "Notepad.exe " + myDir + "/readme.txt"; 
            }
            else 
                app= "gedit " + myDir + "/readme.txt";
            Process p = r.exec(app);
        }catch (Exception e){}
    }//GEN-LAST:event_contentsMenuItemActionPerformed

    private void aboutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutMenuItemActionPerformed
        Icon icon = new javax.swing.ImageIcon(getClass().getResource("/edu/umd/essic/maskbuilder/im.gif"));
        String msg = "GMB version 2.2"+
                    "\n"   + "Software for Grid, Mask Building"+
                    "\n"   + "and data interpolation for performing"+
                    "\n"   + "simulations with SEAPODYM on" +
                    "\n"   + "a mixed-resolution spatial scale."+                    
                    "\n" + "Authors: "+
                    "\n"   + "Inna Senina and Pavel Senin"+
                    "\n"   + "JIMAR PFRP, RCUH 2005."; 
        JOptionPane.showMessageDialog(this,msg,"About",JOptionPane.PLAIN_MESSAGE,icon); 
    }//GEN-LAST:event_aboutMenuItemActionPerformed

    private void FilterMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FilterMenuItemActionPerformed
        int corrected = 10;
        int count = 0;
        while (corrected > 0){
            corrected = mask.CorrectBoundaries();
            count += corrected;
        }
        RefreshDisplay();
        String conf = count + " bad boundary point(s) corrected.";
        JOptionPane.showMessageDialog(this,conf,"Info",JOptionPane.INFORMATION_MESSAGE); 
    }//GEN-LAST:event_FilterMenuItemActionPerformed

    private void ContourShownCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ContourShownCheckBoxActionPerformed
        contour_shown = ContourShownCheckBox.isSelected();
        RefreshDisplay();
    }//GEN-LAST:event_ContourShownCheckBoxActionPerformed

    private void AnyLayerMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AnyLayerMenuItemActionPerformed
        FALSE_RadioButton.setSelected(false);
        TRUE_RadioButton.setSelected(false);
        FALSE_RadioButton.setSelected(false);       
        try {
            String s = JOptionPane.showInputDialog(this,"Enter existing layer flag or negative for EEZ","Mask flag",JOptionPane.DEFAULT_OPTION);
            if (s != null)
                mask_edit_flag = Integer.parseInt(s);
        } catch (NumberFormatException e){return;}
        int nb_layers = mask.GetFlag();
        if (mask_edit_flag>nb_layers) {
            JOptionPane.showMessageDialog(this,"Invalid flag. Will be reset to closest valid one","Warning",JOptionPane.WARNING_MESSAGE);
            mask_edit_flag = nb_layers;
        }
        mask.SetMethod(3);
        //mask_edit_flag --;
        //if (mask_edit_flag < 0) mask_edit_flag = 0;
    }//GEN-LAST:event_AnyLayerMenuItemActionPerformed

    private void EditMaskButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EditMaskButtonActionPerformed
        mask_editable = EditMaskButton.isSelected();
    }//GEN-LAST:event_EditMaskButtonActionPerformed

    private void MaskShownCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MaskShownCheckBoxActionPerformed
        mask_shown = MaskShownCheckBox.getState();
        if (mask_shown){
            itopo_shown = false;
            TopographyShownCheckBox.setSelected(false);
        }
        RefreshDisplay();
    }//GEN-LAST:event_MaskShownCheckBoxActionPerformed

    private void TButtonMaskActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TButtonMaskActionPerformed
        MaskModeCheckBox.setSelected(TButtonMask.isSelected());
        MaskModeCheckBoxActionPerformed(null);
    }//GEN-LAST:event_TButtonMaskActionPerformed

    private void ColCheckBoxMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ColCheckBoxMenuItemActionPerformed
        palette = true;
        BWCheckBoxMenuItem.setSelected(false);
        RefreshDisplay();
    }//GEN-LAST:event_ColCheckBoxMenuItemActionPerformed

    private void BWCheckBoxMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BWCheckBoxMenuItemActionPerformed
        palette = false;
        ColCheckBoxMenuItem.setSelected(false);
        RefreshDisplay();
    }//GEN-LAST:event_BWCheckBoxMenuItemActionPerformed

    private void LoadMaskMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LoadMaskMenuItemActionPerformed
        fc = new JFileChooser(".//output");
        fc.setFileFilter(new FilesFilter(2));
        fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        int returnVal = fc.showDialog(this,"Load land mask only!");
          if (returnVal == JFileChooser.APPROVE_OPTION) {
              try{ 
                  File file = fc.getSelectedFile();
                  BufferedReader r = new BufferedReader(new FileReader(file));
                  int nx = grid.Get_NX();
                  int ny = grid.Get_NY();
                  if (mask != null) mask = null;
                  mask = new Mask(nx-1,ny-1);
                  Point2D.Float[][] coord = grid.Get_Im_Coords();
                  map.ResetThreshold(0);
                  mask.ReadMask(r,nx-1,ny-1,coord,1);
                  mask_exist = true;
                  r.close();
                  RefreshDisplay();
              } catch (IOException e) {
                  System.out.println("Failed task...");    
                  System.exit(0);
              }
           }
        fc = null;       
    }//GEN-LAST:event_LoadMaskMenuItemActionPerformed

    private void ResetThresholdMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ResetThresholdMenuActionPerformed
        int cur_layer = map.GetThreshold();
        cdm.setThreshold(cur_layer);
        cdm.show();
        int threshold = cdm.getThreshold(); 
        if (cdm.OnOK){
            map.ResetThreshold(threshold);
            add_layer = cdm.getStatus();
            if ((mask_exist)&&(add_layer)){
                int flag = cdm.getFlag();
                mask.SetFlag(flag);
            }
        }        
    }//GEN-LAST:event_ResetThresholdMenuActionPerformed

    private void FisheryItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FisheryItemActionPerformed
        if ((mask_exist)&&(grid_exist)){
            fc = new JFileChooser(".//data");
            fc.setFileFilter(new FilesFilter(7));
            int returnValLoad = fc.showDialog(this,"Read fisheries par file");
            if (returnValLoad == JFileChooser.APPROVE_OPTION) {
                File inputpar = fc.getSelectedFile();
                fthread = new FThread(inputpar,grid,cdm.getMaskType()); 
                fthread.start(); 
            }
            fc = null;
            fthread = null;
        }
    }//GEN-LAST:event_FisheryItemActionPerformed

    private void PrintAllMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PrintAllMenuItemActionPerformed
        int answer = JOptionPane.showConfirmDialog(this,"GMB will print the graphics using default printer and program-defined paper settings. Continue?",
                      "Warning",JOptionPane.YES_NO_OPTION); // returns 0 if YES has been chosen, 1 if NO
        if (answer == 0){
            MyPrint mp = new MyPrint(ImageLabel,map.GetWidth(),map.GetHeight());
            mp=null;
        }
    }//GEN-LAST:event_PrintAllMenuItemActionPerformed

    private void LoadSavedDataMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LoadSavedDataMenuItemActionPerformed
        fc = new JFileChooser(".//output");
        fc.setFileFilter(new FilesFilter(1));
        fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        int returnVal = fc.showDialog(this,"Load GMB data!");
          if (returnVal == JFileChooser.APPROVE_OPTION) {
              if (contour != null)
                  contour = null;
                  contour_shown = false;
                  ContourShownCheckBox.setEnabled(false);
              try{ 
                  File file = fc.getSelectedFile();
                  BufferedReader r = new BufferedReader(new FileReader(file));
                  MapFileName = r.readLine();
                  mfd.ReadMapData(r);
                  File f = new File(MapFileName);
                  LoadMap(f,true);
                  if ((map_exist)&&(mfd.OnOK)&&(!map.getFileReadStatus())){
                      MenuChecker();
                      String line = r.readLine();
                      Pos p = new Pos();
                      int funcnum = p.ReadInt(line);
                      int nx = p.ReadInt(line);
                      int ny = p.ReadInt(line);
                      int mask_type = p.ReadInt(line);
                      cdm.setMaskType(mask_type);
                      p = null;
                      int w = map.GetWidth();
                      int h = map.GetHeight();
                      float left_x = map.getBounds()[0];
                      if (left_x < 0) left_x = left_x + 360;
                      grid = new Grid(w,h,nx,ny,left_x,map.getBounds()[2], map.getNLON(), map.getNLAT());
                      grid.ReadGridData(r,funcnum);
                      switch (funcnum){
                          case 0:
                              Point NumNodes = new Point(nx,ny);
                              CreateRegularGrid(NumNodes);
                              break;
                          case 1:
                              CreateEssicGrid();
                              break;
                          case 2:
                              Create2DGrid();
                              break;
                          case 3:
                              CreateComboGrid();
                              break;
                      }
                      MenuChecker();
                      if (mask_type == 0){
                          nx = nx+1;
                          ny = ny+1;
                      }
                      mask = new Mask(nx-1,ny-1);
                      Point2D.Float[][] coord = grid.Get_Im_Coords();
                      line = r.readLine();
                      map.ResetThreshold(Integer.parseInt(line));
                      mask.ReadMask(r,nx-1,ny-1,coord,mask_type);
                      mask_exist = true;
                      r.close();
                      RefreshDisplay();
                      ShowGridInfo(funcnum);
                      String str = InfoField.getText() + "; Mask exist, current ocean flag is " + mask.GetFlag();
                      InfoField.setText(str);
                  }
              } catch (IOException e) {
                  System.out.println("Failed task...");    
                  System.exit(0);
              }
           }
        fc = null;       
    }//GEN-LAST:event_LoadSavedDataMenuItemActionPerformed

    private void ImageLabelMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ImageLabelMouseMoved
        if (map_exist){
            float x = evt.getX()/zoom_level;
            float y = evt.getY()/zoom_level;
            String str = map.getCoordstr(x,y);
            if ((x<map.GetWidth()) && (y<map.GetHeight()))
                CoordinateField.setText(str);
            else CoordinateField.setText(" ");
        }
    }//GEN-LAST:event_ImageLabelMouseMoved

    private void ImageLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ImageLabelMouseClicked
        if (mask_exist && mask_editable) { 
            int x = Math.round(evt.getX()/zoom_level);
            int y = Math.round(evt.getY()/zoom_level);
            Point2D.Float crd = map.getCoord(x,y);
            Point ij = grid.get_index(crd.x,crd.y);
            mask.ChangeFlag(ij,mask_edit_flag);
            Image im = imageicon.getImage();
            Graphics g = im.getGraphics();
            imageicon.paintIcon(this,g,0,0);
            EditMask(g,ij.x,ij.y);
            imageicon.setImage(new ImageIcon(im).getImage());
            ImageLabel.setIcon(imageicon);
            MyScrollPane.getViewport().add(ImageLabel);
            g.dispose();
        }
    }//GEN-LAST:event_ImageLabelMouseClicked

    private void EditMask(Graphics g, int i, int j){
        int k=mask.CheckNode(i,j);
        if (k<0) k = mask.GetFlag()+1;
        if (patch_mode){
            Polygon p = grid.GetPolygon(i,j,zoom_level,grid_shown);
            g.setColor(mask_colors[k]);
            g.fillPolygon(p);
        }else {
            g.setColor(mask_colors[k]);
            Point node = mask.GetNode(i,j,zoom_level);
            g.fillOval(node.x-2,node.y-2,4,4);
        }
    }
    
    private void ImageLabelMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ImageLabelMousePressed
        //if ((TButtonScale.isSelected())||(TButtonMask.isSelected())||(TButtonMap.isSelected())){
        if (grid_exist){
            int x = Math.round(evt.getX()/zoom_level);
            int y = Math.round(evt.getY()/zoom_level);
            draggedrect = new Rectangle(x,y,0,0);
        }   
    }//GEN-LAST:event_ImageLabelMousePressed

    private void ImageLabelMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ImageLabelMouseDragged
        //if ((TButtonScale.isSelected())||(TButtonMask.isSelected())||(TButtonMap.isSelected())){
        if ((grid_exist)&&((TButtonScale.isSelected())||(EditMaskButton.isSelected()))){
            int x = Math.round(evt.getX()/zoom_level);
            if (x > map.GetWidth()) x = map.GetWidth();
            int y = Math.round(evt.getY()/zoom_level);
            if (y > map.GetHeight()) y = map.GetHeight();
            if (x < ImageLabel.getX()) x = ImageLabel.getX();
            if (y < ImageLabel.getY()) y = ImageLabel.getY();
            Graphics g = ImageLabel.getGraphics();
            imageicon.paintIcon(this,g,0,0);
            g.setColor(Color.MAGENTA);
            Polygon p = new Polygon();
            p.addPoint((int)(draggedrect.x*zoom_level),(int)(draggedrect.y*zoom_level));
            p.addPoint((int)(x*zoom_level),(int)(draggedrect.y*zoom_level));
            p.addPoint((int)(x*zoom_level),(int)(y*zoom_level));
            p.addPoint((int)(draggedrect.x*zoom_level),(int)(y*zoom_level));
            g.drawPolygon(p);
            g.dispose();
            draggedrect.setSize(Math.abs(x-draggedrect.x),Math.abs(y-draggedrect.y));
       }
    }//GEN-LAST:event_ImageLabelMouseDragged

    private Rectangle UpdateDraggedRect(int x, int y){
        Rectangle r = new Rectangle(0,0);
        int x_init = (int)(draggedrect.x);
        int y_init = (int)(draggedrect.y);
        int w      = draggedrect.width;
        int h      = draggedrect.height;
        if (x>x_init){
            if (y>y_init)
                r = draggedrect;
            else
                r.setRect(x_init, y, w, h);
        }else{
            if (y>draggedrect.getY())
                r.setRect(x, y_init, w, h);
            else
                r.setRect(x, y, w, h);
        }
        return r;
    }
    
    private void ImageLabelMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ImageLabelMouseReleased
       int x = 0;
       int y = 0;
       if (map_exist || grid_exist || mask_exist){
        x = Math.round(evt.getX()/zoom_level);
        if (x > map.GetWidth()) x = map.GetWidth();
        y = Math.round(evt.getY()/zoom_level);
        if (y > map.GetHeight()) y = map.GetHeight();
        if (x < ImageLabel.getX()) x = ImageLabel.getX();
        if (y < ImageLabel.getY()) y = ImageLabel.getY();
       
       if ((draggedrect != null)&&
            (x != draggedrect.getX())&&(y != draggedrect.getY())){
        if (mask_shown && mask_editable){
            draggedrect = UpdateDraggedRect(x,y);
            int x1 = (int)(draggedrect.x);
            int y1 = (int)(draggedrect.y);
            int x2 = x1+draggedrect.width;
            int y2 = y1+draggedrect.height;
            Point2D.Float crd1 = map.getCoord(x1,y1);
            Point2D.Float crd2 = map.getCoord(x2,y2);
            Point[] ij = new Point[2];
            ij[0] = grid.get_index(crd1.x,crd1.y);
            ij[1] = grid.get_index(crd2.x,crd2.y);
            //check last index to avoid 'out of bounds' error, occuring due to truncation
            if (ij[0].x>mask.GetLonDim()) ij[0].x = mask.GetLonDim(); 
            if (ij[0].y>mask.GetLatDim()) ij[0].y = mask.GetLatDim(); 
            if (ij[1].x>mask.GetLonDim()) ij[1].x = mask.GetLonDim(); 
            if (ij[1].y>mask.GetLatDim()) ij[1].y = mask.GetLatDim();            
            mask.ChangeFlag(ij,mask_edit_flag);
            RefreshDisplay();
         }
              
        if (grid_exist && (TButtonScale.isSelected())){
            map.StoreRectData(UpdateDraggedRect(x,y));
            BufferedImage image = map.ReturnImage(false);
            if (zoom_level != 1){
                int w = Math.round(map.GetWidth()*zoom_level);
                int h = Math.round(map.GetHeight()*zoom_level);
                Image si = image.getScaledInstance(w, h, Image.SCALE_FAST);
                image = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
                Graphics2D g = image.createGraphics();
                g.drawImage(si, 0, 0, null);
                g.dispose();
            }
            Graphics2D drawing = image.createGraphics();
            drawing.setColor(Color.BLACK);
              int size_x = grid.Get_NX();
              int size_y = grid.Get_NY();
              double zoom_x = zoom_level*((map.GetWidth()*0.1)/(draggedrect.width*0.1));
              double zoom_y = zoom_level*((map.GetHeight()*0.1)/(draggedrect.height*0.1));
              Point2D.Float[][] coord = grid.Get_Im_Coords();
              for(int i=0;i<size_x-1;i++)
                for(int j=0;j<size_y-1;j++){
                    if (draggedrect.contains(coord[i][j].x,coord[i][j].y)){
                        Polygon p = new Polygon();
                        p.addPoint((int)Math.round((coord[i][j].x-draggedrect.x)*zoom_x),(int)Math.round((coord[i][j].y-draggedrect.y)*zoom_y));
                        p.addPoint((int)Math.round((coord[i+1][j].x-draggedrect.x)*zoom_x),(int)Math.round((coord[i+1][j].y-draggedrect.y)*zoom_y));
                        p.addPoint((int)Math.round((coord[i+1][j+1].x-draggedrect.x)*zoom_x),(int)Math.round((coord[i+1][j+1].y-draggedrect.y)*zoom_y));
                        p.addPoint((int)Math.round((coord[i][j+1].x-draggedrect.x)*zoom_x),(int)Math.round((coord[i][j+1].y-draggedrect.y)*zoom_y));
                        drawing.drawPolygon(p);
                    }
                }           
            drawing.dispose();   
            imageicon.setImage(new ImageIcon(image).getImage());
            ImageLabel.setIcon(imageicon);
            MyScrollPane.getViewport().removeAll();
            MyScrollPane.getViewport().add(ImageLabel);
            TButtonScale.setEnabled(false);
            TButtonScale.setSelected(false);
        }
       }
        }
    }//GEN-LAST:event_ImageLabelMouseReleased

    private void TButtonScaleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TButtonScaleActionPerformed
        TButtonScale.setSelected(true);
        EditMaskButton. setSelected(false);
        mask_editable = false;
    }//GEN-LAST:event_TButtonScaleActionPerformed

    private void SaveAllMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveAllMenuItemActionPerformed
        fc = new JFileChooser(".//output");
        fc.setFileFilter(new FilesFilter(1));
        fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        int returnVal = fc.showDialog(this,"Save all data!");
          if (returnVal == JFileChooser.APPROVE_OPTION) {
           try{ 
              File file = fc.getSelectedFile(); 
              String fname = fc.getName(file);                
              if (fname.indexOf(".gmb")==-1){
                String path = file.getAbsolutePath();
                String new_name = path + ".gmb";
                file = new File(new_name);
              }               
              FileWriter out = new FileWriter(file);
              if (grid_exist){
                  int size_x = grid.Get_NX();
                  int size_y = grid.Get_NY();
                  out.write(MapFileName);
                  out.write(NEWLINE);
                  map.SaveMapData(out);
                  out.write(NEWLINE);
                  out.write(String.valueOf(grid.Get_StrType())); out.write("\t"); 
                  out.write(String.valueOf(size_x)); out.write("\t");
                  out.write(String.valueOf(size_y)); out.write("\t");
                  int mask_type = 1; 
                  if (!cdm.getMaskType()) mask_type = 0; 
                  out.write(String.valueOf(mask_type)); out.write("\t");
                  out.write(NEWLINE);
                  if (grid.Get_StrType() == 1){
                      grid.SaveEssicGridData(out);
                  } else if (grid.Get_StrType() == 2){
                      grid.SaveExp2dGridData(out);
                  } else if (grid.Get_StrType() == 3){
                      grid.SaveEssicGridData(out);
                      grid.SaveExp2dGridData(out);
                  }
                  if (mask_exist){
                      int threshold = map.GetThreshold();
                      out.write(String.valueOf(threshold)); out.write("\n");
                      if (!cdm.getMaskType()){
                          size_x = size_x+1;
                          size_y = size_y+1;
                      }
                      for(int j=0;j<size_y-1; j++){
                         for(int i=0;i<size_x-1;i++){
                             out.write(String.valueOf(mask.CheckNode(i,j)));
                             out.write("\t");
                         }
                         out.write(NEWLINE);
                       }
                   }
              }
              out.close();
           } catch (IOException e) {
            System.out.println("Failed task...");    
            System.exit(0);}
          }
        fc = null;       
    }//GEN-LAST:event_SaveAllMenuItemActionPerformed

    
    private void InterpolationItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_InterpolationItemActionPerformed
        if ((mask_exist)&&(grid_exist)){
            int func = grid.Get_StrType();
            Point2D.Double[][] coord = grid.Get_Re_Coords();
            int nx = grid.Get_NX();
            int ny = grid.Get_NY();
            double[][] mx = new double[nx][ny];
            double[][] my = new double[nx][ny];
            grid.GetMetrics(mx,my);
            if (!cdm.getMaskType()){
                nx = nx+1;
                ny = ny+1;
            }
            int[][] msk = new int[nx-1][ny-1];
            for (int i=0;i<nx-1;i++)
                for (int j=0;j<ny-1;j++){
                    msk[i][j] = mask.CheckNode(i,j);
                }
            fc = new JFileChooser(".//data");
            fc.setFileFilter(new FilesFilter(4));
            //fc.setMultiSelectionEnabled(true);
            int returnValLoad = fc.showDialog(this,"Read Dym file");
            if (returnValLoad == JFileChooser.APPROVE_OPTION) {
                File inputBin = fc.getSelectedFile();               
                int returnValSave = fc.showDialog(this,"Write data to dym file");
                if (returnValSave == JFileChooser.APPROVE_OPTION) {
                    //File path = fc.getCurrentDirectory();            
                    File outputBin = fc.getSelectedFile();
                    //RefreshDisplay();
                    ithread = new IThread(FileDlg,inputBin,outputBin,func,msk,nx-1,ny-1,coord,mx,my,cdm.getMaskType()); 
                    if (ithread.isError()) return;
                    ithread.start();
                    boolean MaskChanged = ithread.mask_changed;
                    if (MaskChanged){
                        msk = ithread.mask;
                        int x = 0; int y = 0;
                        for(int i=0; i<nx-1; i++){
                        for(int j=0;j<ny-1;j++){
                            if (cdm.getMaskType()){
                                x = (int) Math.round((coord[i+1][j].x+coord[i][j].x)/2);
                                y = (int) Math.round((coord[i][j+1].y+coord[i][j].y)/2);
                            } else {
                                x = (int) Math.round(coord[i][j].x);
                                y = (int) Math.round(coord[i][j].y);
                            }  
                            mask.SetNode(i,j,x,y,msk[i][j]);
                        }
                        }
                        RefreshDisplay();
                    }
                } 
            }
            fc = null;
            ithread = null;
        }
    }//GEN-LAST:event_InterpolationItemActionPerformed
   
    private void SaveGridMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveGridMenuItemActionPerformed
       if(grid_exist){
        //filechoser dialog
        fc = new JFileChooser(".//output");
        fc.setFileFilter(new FilesFilter(2));
        fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        int returnVal = fc.showDialog(this,"Save grid data");
          if (returnVal == JFileChooser.APPROVE_OPTION) {
           try{ 
              File file = fc.getSelectedFile();
              FileWriter out = new FileWriter(file);
              int size_x = grid.Get_NX();
              int size_y = grid.Get_NY();
              Point2D.Double[][] coord = grid.Get_Re_Coords();
              float LatMin = (float)coord[0][size_y-1].y;//+grid.Get_StepY()/2;
              float LatMax = (float)coord[0][0].y;//-grid.Get_StepY()/2;
              float LonMin = (float)coord[0][0].x;//+grid.Get_StepX()/2;
              float LonMax = (float)coord[size_x-1][0].x;//-grid.Get_StepX()/2;
              out.write(String.valueOf(grid.Get_StrType())); out.write("\t"); 
              out.write(String.valueOf(size_x)); out.write("\t");
              out.write(String.valueOf(size_y)); out.write("\t");
             /* out.write(String.valueOf(LatMin)); out.write("\t");
              out.write(String.valueOf(LatMax)); out.write("\t");
              out.write(String.valueOf(LonMin)); out.write("\t");
              out.write(String.valueOf(LonMax)); out.write("\t");*/
              out.write(NEWLINE);
              if (grid.Get_StrType()!=0){
               for(int j=0;j<size_y;j++){
                    for(int i=0; i<size_x; i++){
                        out.write(String.valueOf(coord[i][j].x));  out.write("\t");
                    }
                  out.write(NEWLINE);
                }
               for(int j=0;j<size_y;j++){
                    for(int i=0; i<size_x; i++){
                        out.write(String.valueOf(coord[i][j].y));  out.write("\t");
                    }
                  out.write(NEWLINE);
                }
               double[][] mx = new double[size_x][size_y];
               double[][] my = new double[size_x][size_y];
               grid.GetMetrics(mx,my);
               for(int j=1;j<size_y;j++){
                    for(int i=1; i<size_x; i++){
                        out.write(String.valueOf(mx[i][j]));  out.write("\t");
                    }
                  out.write(NEWLINE);
                }
               for(int j=1;j<size_y;j++){
                    for(int i=1; i<size_x; i++){
                        out.write(String.valueOf(my[i][j]));  out.write("\t");
                    }
                  out.write(NEWLINE);
                }
              }
               out.close();
           }catch (IOException e) {
            System.out.println("Failed task...");    
            System.exit(0);}
          }
        fc = null;
       } 
    }//GEN-LAST:event_SaveGridMenuItemActionPerformed

    private void LoadContourMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LoadContourMenuItemActionPerformed
        // TODO add your handling code here:
       fc = new JFileChooser(".//maps");
       fc.setFileFilter(new FilesFilter(3));
       double Scale = 1.0;
       int returnVal = fc.showDialog(this,"Load Contour");
       if (returnVal == JFileChooser.APPROVE_OPTION) {
              File file = fc.getSelectedFile();
              Scale = map.GetWidth()/map.getNLON();
              contour = new Contour(file, Scale, map.getBounds()[0], map.getBounds()[2],map.getNLON(),map.getNLAT());
              contour_exist = true;
          }
       RefreshDisplay();
       fc = null;
    }//GEN-LAST:event_LoadContourMenuItemActionPerformed

    private void Stretch2DMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Stretch2DMenuItemActionPerformed
        // TODO add your handling code here:
    if(grid_exist){
        //if (Dialog2d != null) Dialog2d = null;
        //Dialog2d = new Dialog_2D(this, true,1,false);
        //Dialog2d.setLocationRelativeTo(this);
        //Dialog2d.setLocation(20,20);        
        Dialog2d.show();
        if (Dialog2d.OnOK){
            Point2D.Float[] Skew = Dialog2d.getSkew();
            Point2D.Float[] Dim = Dialog2d.getDim();
            int Nreg = Dialog2d.getNumreg();
            Point2D.Float[] Point_Cen = Dialog2d.getCenter();
            grid.FinalizeStretchVariables();
            grid.Init2DStretching(Nreg);
            grid.SetStretchedAreas(Point_Cen,Skew,Dim);
            //grid.Build();
            Create2DGrid();
            RefreshDisplay();
        }
    }
    }//GEN-LAST:event_Stretch2DMenuItemActionPerformed

    private void Create2DGrid(){
        grid.InitStretchVariables();
        grid.Stretch(2);
        grid.Stretch(2);
        grid_exist = true;
        if (mask_exist){
            mask = null;
            mask_exist = false;      
        }
        ShowGridInfo(2);
    }
    
    private void ShowGridInfo(int type){
        String str = "";
        if (grid_exist){
            int nx = grid.Get_NX();
            int ny = grid.Get_NY();
            if (type>0){
                float minstep_x = grid.Get_MinDX();
                float maxstep_x = grid.Get_MaxDX();
                float minstep_y = grid.Get_MinDY();
                float maxstep_y = grid.Get_MaxDY();
                str = "Mixed-resolution grid: " + nx + "x" + ny + ", resolution in degrees (min,max): ( " 
                    + minstep_x + " , " + maxstep_x + ")x(" + minstep_y + " , " + maxstep_y + ")";    
            } else{
                float step_x = grid.Get_StepX();
                float step_y = grid.Get_StepY();
                str = "Regular grid: " + nx + "x" + ny + ", resolution in degrees: " + step_x + "x" + step_y;
            }
        } else str = "No grid and mask exist";
        InfoField.setText(str); 
    }
    
    private void CleanGridInfo(){
        InfoField.setText("No grid and mask exist");
    }
    
    private void EssicStretchMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EssicStretchMenuItemActionPerformed
     // TODO add your handling code here:
        if(grid_exist){
            EssicDlg.show();
            if (EssicDlg.OnOK){
                float result[][] = EssicDlg.getReturnStatus();
                int Nmax = EssicDlg.NMAX;
                float alpha_x[] = new float[Nmax];
                float beta_x[] = new float[Nmax];
                float gamma_x[] = new float[Nmax];
                float alpha_y[] = new float[Nmax];
                float beta_y[] = new float[Nmax];
                float gamma_y[] = new float[Nmax];
   
                for(int k=0;k<Nmax;k++){
                    alpha_x[k] = result[k][0];
                    beta_x[k] = result[k][1];
                    gamma_x[k] = result[k][2];
                    alpha_y[k] = result[k][3];
                    beta_y[k] = result[k][4];
                    gamma_y[k] = result[k][5];
                }
                grid.FinalizeStretchVariables();
                grid.InitEssicStretching(Nmax);
                grid.SetEssicCoefficients(alpha_x, beta_x, gamma_x, alpha_y, beta_y, gamma_y, EssicDlg.flag);
                CreateEssicGrid();
                RefreshDisplay();
            }
        }
    }//GEN-LAST:event_EssicStretchMenuItemActionPerformed

    private void CreateEssicGrid(){
        grid.InitStretchVariables();
        grid.Stretch(1);
        grid_exist = true;
        if (mask_exist){
            mask = null;
            mask_exist = false;
            mask_shown = false;          
        }
        ShowGridInfo(1);
    }
    
    private void FALSE_RadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FALSE_RadioButtonActionPerformed
        XOR_RadioButton.setSelected(false);
        TRUE_RadioButton.setSelected(false);
        FALSE_RadioButton.setSelected(true);
        mask.SetMethod(3);
        mask_edit_flag = mask.GetFlag();
    }//GEN-LAST:event_FALSE_RadioButtonActionPerformed

    private void TRUE_RadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TRUE_RadioButtonActionPerformed
        XOR_RadioButton.setSelected(false);
        TRUE_RadioButton.setSelected(true);
        FALSE_RadioButton.setSelected(false);
        mask.SetMethod(2);
        mask_edit_flag = mask.GetFlag();
    }//GEN-LAST:event_TRUE_RadioButtonActionPerformed

    private void XOR_RadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_XOR_RadioButtonActionPerformed
        XOR_RadioButton.setSelected(true);
        TRUE_RadioButton.setSelected(false);
        FALSE_RadioButton.setSelected(false);
        mask.SetMethod(1);
        mask_edit_flag = mask.GetFlag();
    }//GEN-LAST:event_XOR_RadioButtonActionPerformed

    private void MaskModeCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MaskModeCheckBoxActionPerformed
        patch_mode = MaskModeCheckBox.isSelected();
        TButtonMask.setSelected(patch_mode);
        RefreshDisplay();
    }//GEN-LAST:event_MaskModeCheckBoxActionPerformed

    private void ZoomOutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ZoomOutMenuItemActionPerformed
        if (zoom_level >= ZOOM_MIN+scale_factor*ZOOM_INIT){ 
            zoom_level = zoom_level - (float)(ZOOM_INIT*scale_factor);
            ZoomInMenuItem.setEnabled(true);
            ButtonZoomIn.setEnabled(true);
            if (zoom_level <= ZOOM_MIN+scale_factor*ZOOM_INIT){
                ZoomOutMenuItem.setEnabled(false);
                ButtonZoomOut.setEnabled(false);
            }
        } 
        RefreshDisplay();
    }//GEN-LAST:event_ZoomOutMenuItemActionPerformed

    private void restoreMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_restoreMenuItemActionPerformed
        zoom_level = ZOOM_INIT;
        TButtonScale.setEnabled(true);
        RefreshDisplay();
    }//GEN-LAST:event_restoreMenuItemActionPerformed

    private void StretchCombMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StretchCombMenuItemActionPerformed
        if(grid_exist){
            EssicDlg.show();
            if (EssicDlg.OnOK){
                float result[][] = EssicDlg.getReturnStatus();
                int Nmax = EssicDlg.NMAX;
                float alpha_x[] = new float[Nmax];
                float beta_x[]  = new float[Nmax];
                float gamma_x[] = new float[Nmax];
                float alpha_y[] = new float[Nmax];
                float beta_y[]  = new float[Nmax];
                float gamma_y[] = new float[Nmax];
                for(int k=0;k<Nmax;k++){
                    alpha_x[k] = result[k][0];
                    beta_x[k]  = result[k][1];
                    gamma_x[k] = result[k][2];
                    alpha_y[k] = result[k][3];
                    beta_y[k]  = result[k][4];
                    gamma_y[k] = result[k][5];
                }
                int Nreg = 0;
                for (int k=0;k<Nmax;k++){
                    if ((alpha_x[k]!=0.0)&&(alpha_y[k]!=0.0))
                    Nreg++; 
                }
                Point2D.Float[] Point_Cen = new Point2D.Float[Nreg];
                for (int k=0; k<Nreg; k++){
                    Point_Cen[k]   = new Point2D.Float(0.f,0.f);
                    Point_Cen[k].x = gamma_x[k];
                    Point_Cen[k].y = gamma_y[k];
                }
                //if (Dialog2d != null) Dialog2d = null;
                //Dialog2d = new Dialog_2D(this,true,Nreg,true);
                //Dialog2d.setLocationRelativeTo(this);
                //Dialog2d.setLocation(20,20); 
                Dialog2d.show();
                if (Dialog2d.OnOK){ 
                    grid.FinalizeStretchVariables();
                    grid.InitEssicStretching(Nmax);
                    grid.SetEssicCoefficients(alpha_x, beta_x, gamma_x, alpha_y, beta_y, gamma_y, EssicDlg.flag);
                    Point2D.Float[] Skew = Dialog2d.getSkew();
	            Point2D.Float[] Dim = Dialog2d.getDim();
	            int Nreg2d = Dialog2d.getNumreg();
        	    Point2D.Float[] Point_Cen2d = Dialog2d.getCenter();
                    grid.Init2DStretching(Nreg2d);
                    grid.SetStretchedAreas(Point_Cen2d,Skew,Dim);
                    CreateComboGrid();
                    RefreshDisplay();
                }
            }
        }        
    }//GEN-LAST:event_StretchCombMenuItemActionPerformed
    public void CreateComboGrid(){
        grid.InitStretchVariables();
        grid.Stretch(3);
        grid.Stretch(3);
        grid_exist = true;
        if (mask_exist){
            mask = null;
            mask_exist = false;
            mask_shown = false;    
        }
        ShowGridInfo(3);
    }
    
    private void SaveMaskMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveMaskMenuItemActionPerformed
       if(mask_exist){
        fc = new JFileChooser(".//output");
        fc.setAcceptAllFileFilterUsed(false);
        fc.addChoosableFileFilter(new FilesFilter(2));
        int returnVal = fc.showDialog(this,"Save mask");
          if (returnVal == JFileChooser.APPROVE_OPTION) {
           try{ 
              File file = fc.getSelectedFile();
              FileWriter out = new FileWriter(file);
              int size_x = grid.Get_NX();
              int size_y = grid.Get_NY();
              if (!cdm.getMaskType()){
                  //for bilding node centered mask: 
                  //since tophography map resolutions 
                  //do not allow to extract map starting 
                  //from 0.25 degree)
                  size_x += 1;
                  size_y += 1;
              }
              for(int j=0;j<size_y-1; j++){
                for(int i=0;i<size_x-1;i++){
                    int val = mask.CheckNode(i,j);
                    String str = String.valueOf(val); 
                    out.write(str);
                    out.write("\t");
                }
              out.write(NEWLINE);
              }
               out.close();
           }catch (IOException e) {
            System.out.println("Failed task...");    
            System.exit(0);}

          }
        fc = null;
       } 
    }//GEN-LAST:event_SaveMaskMenuItemActionPerformed

    private void BuildMaskMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BuildMaskMenuItemActionPerformed
        if(map_exist && grid_exist){
            int size_x = grid.Get_NX();
            int size_y = grid.Get_NY();
            int x = 0;
            int y = 0;
            Point2D.Float[][] coord = grid.Get_Im_Coords();
            boolean mask_type = cdm.getMaskType();
            if (!add_layer){
                mask = null;
                if (!mask_type){
                    //for bilding node centered mask: 
                    //since tophography map resolutions 
                    //do not allow to extract map starting 
                    //from 0.25 degree)
                    size_x += 1;
                    size_y += 1;
                }
                mask = new Mask(size_x-1, size_y-1);
                mask.ResetFlag();
                int flag = 1;
                    for(int i=0; i<size_x-1; i++){
                        for(int j=0;j<size_y-1;j++){
                            if (mask_type){
                                x = Math.round((coord[i+1][j].x+coord[i][j].x)/2);
                                y = Math.round((coord[i][j+1].y+coord[i][j].y)/2);
                            } else {
                                x = Math.round(coord[i][j].x);
                                y = Math.round(coord[i][j].y);
                            }  
                            Polygon p = grid.GetPolygon(i,j);
                            if (map.CheckDepth(p)){
                                mask.SetNode(i,j,x,y,flag);
                            }else
                                mask.SetNode(i,j,x,y,0);                            
                        }
                    }
                //mask.CorrectBoundaries();
                mask_exist  = true;
                mask_edit_flag = mask.GetFlag();
                TButtonScale.setSelected(false);
            } else {
                if (mask_exist){
                    int flag = mask.GetFlag();
                    for(int i=0; i<size_x-1; i++){
                        for(int j=0;j<size_y-1;j++){
                            if (mask_type){
                                x = Math.round((coord[i+1][j].x+coord[i][j].x)/2);
                                y = Math.round((coord[i][j+1].y+coord[i][j].y)/2);
                            } else {
                                x = Math.round(coord[i][j].x);
                                y = Math.round(coord[i][j].y);
                            }                              
                            Polygon p = grid.GetPolygon(i,j);
                            if ((map.CheckDepth(p))&&(mask.CheckNode(i,j)!=0))
                                mask.SetNode(i,j,x,y,flag);   
                        }
                    }
                    //mask.CorrectBoundaries();
                    mask.SetMultiProperty(true);
                    add_layer = false;
                    mask_edit_flag = mask.GetFlag();
                }
            }
            RefreshDisplay();
            String str = "";
            if (mask_exist){
                ShowGridInfo(grid.Get_StrType());
                str = InfoField.getText() + "; Mask exist, current ocean flag is " + mask.GetFlag();
                InfoField.setText(str);
            }
        }  
    }//GEN-LAST:event_BuildMaskMenuItemActionPerformed
/*
 *if (TButtonMap.isEnabled()){
                    for(int i=0; i<size_x-1; i++){
                        for(int j=0;j<size_y-1;j++){
                            if (mask_type){
                                x = Math.round((coord[i+1][j].x+coord[i][j].x)/2);
                                y = Math.round((coord[i][j+1].y+coord[i][j].y)/2);
                            } else {
                                x = Math.round(coord[i][j].x);
                                y = Math.round(coord[i][j].y);
                            }                       
                            if (map.CheckColor(x,y)){
                                mask.SetNode(i,j,x,y,flag);
                            }else{
                                mask.SetNode(i,j,x,y,0);                                
                            }
                        }
                    }    
                }else{
                            if (mask_type){
                                x = Math.round((coord[i+1][j].x+coord[i][j].x)/2);
                                y = Math.round((coord[i][j+1].y+coord[i][j].y)/2);
                            } else {
                                x = Math.round(coord[i][j].x);
                                y = Math.round(coord[i][j].y);
                            }   
 if((i>0)&&(j>0)&&(i<size_x-2)&&(j<size_y-2)){
                                    // Check depth in neighbor nodes
                                    int xl,xr,yu,yb;
                                    if (mask_type){
                                        xl = Math.round((coord[i-1][j].x+coord[i][j].x)/2);
                                        xr = Math.round((coord[i+1][j].x+coord[i+2][j].x)/2);
                                        yu = Math.round((coord[i][j-1].y+coord[i][j].y)/2);
                                        yb = Math.round((coord[i][j+1].y+coord[i][j+2].y)/2);
                                    } else {
                                        xl = Math.round(coord[i-1][j].x);
                                        xr = Math.round(coord[i+1][j].x);
                                        yu = Math.round(coord[i][j-1].y);
                                        yb = Math.round(coord[i][j+1].y);
                                    }
                                    int count = map.CheckNeighbors(x,y,xl,xr,yu,yb);
                                    if (count >2)
                                        mask.SetNode(i,j,x,y,0);
                                }
*/
    private void GenMaskColors(){
        if (mask_exist){
            nb_layers = mask.GetFlag();
            mask_colors = new Color[nb_layers+2]; 
            if (palette){
                if (!patch_mode)
                    mask_colors[0] = Color.YELLOW;
                else
                    mask_colors[0] = new Color(0,250,10);
                double step;
                if (nb_layers!=1) step = (double)255/(nb_layers-1); 
                else step = 255;
                for (int i=1; i<mask_colors.length; i++){
                     int k = map.ocean_ncol-i*map.ocean_ncol/mask_colors.length;
                    mask_colors[i] = map.ocean_col[k-1];
                }
                if (nb_layers>1)
                    mask_colors[1] = map.ocean_col[map.ocean_ncol-1];
            } else {
                int b;
                mask_colors[0] = Color.BLACK;
                double step = (double)255/nb_layers; 
                for (int i=1; i<nb_layers+1; i++){
                    b = (int)(i*step);
                    mask_colors[i] = new Color(b,b,b);
                }
            }
            //one more color for negative mask flag
            mask_colors[mask_colors.length-1] = new Color(255, 100, 200);
        }
 }
    
    private void LoadMapMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LoadMapMenuItemActionPerformed
      /* fc = new JFileChooser(".");
       WorkingDir = fc.getCurrentDirectory().getPath();
       fc = null;*/
       contour = null;
       fc = new JFileChooser(".//maps");
       fc.setFileFilter(new FilesFilter(5));
       int returnVal = fc.showDialog(this,"Load GEODAS map");
       if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            MapFileName = file.getAbsolutePath();
            LoadMap(file,false);
            if (map_exist){
                RefreshDisplay();
            }
        }
       fc = null;
       if (!grid_exist) 
           CleanGridInfo();
    }//GEN-LAST:event_LoadMapMenuItemActionPerformed

    private void LoadMap(File file, boolean NoDialog){
       if(map == null){
                map = new Map(file,mfd,NoDialog);   
                if ((mfd.OnOK)&&(!map.getFileReadStatus())){
                    BufferedImage displayimage;
                    map.LoadFileData(file,mfd);
                    ZOOM_MAX = 3.f;
                    //if (ZOOM_MAX/3 < 1.0) ZOOM_INIT  = (float)(ZOOM_MAX/3);
                    ZOOM_INIT = (float)FixedWidth/map.GetWidth();
                    if (ZOOM_INIT > 1) ZOOM_INIT = 1;
                    zoom_level = ZOOM_INIT;
                    if(zoom_level != 1){
                        int w = Math.round(map.GetWidth()*zoom_level);
                        int h = Math.round(map.GetHeight()*zoom_level);
                        BufferedImage bi = map.ReturnImage(true);
                        Image si = bi.getScaledInstance(w, h, Image.SCALE_FAST);
                        displayimage = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
                        Graphics2D g = displayimage.createGraphics();
                        g.drawImage(si, 0, 0, null);
                        g.dispose();
                    }else{
                        displayimage = map.ReturnImage(true);
                    }
                    imageicon = new ImageIcon(displayimage);
                    ImageLabel.setIcon(imageicon);
                    ImageLabel.setText(null);
                    ImageLabel.setHorizontalAlignment(SwingConstants.LEFT);
                    ImageLabel.setVerticalAlignment(SwingConstants.TOP);
                    MyScrollPane.getViewport().add(ImageLabel);
                    map_exist = true; 
                    ZoomInMenuItem.setEnabled(true);
                    ZoomOutMenuItem.setEnabled(true);                  
                }
                else KillClasses();
       }else{
           map.LoadFileHeader(file,mfd,NoDialog);
           if ((mfd.OnOK)&&(!map.getFileReadStatus())){
                map.LoadFileData(file,mfd);
                map.ResetThreshold(0);
                grid=null;
                grid_exist = false;
                mask = null;
                mask_exist = false;
                contour = null;
                contour_exist = false;
                ZOOM_MAX = 3.f;
                ZOOM_INIT = (float)FixedWidth/map.GetWidth();
                if (ZOOM_INIT > 1) ZOOM_INIT = 1;
                zoom_level = ZOOM_INIT;              
           }else return;
       }
    }
    
    private void KillClasses(){
        map = null; map_exist = false;
        grid = null; grid_exist = false;
        mask = null; mask_exist = false;
    }
    /* private void PatchMapMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
        if((grid_exist)&&(map_exist)){
            int size_x = grid.Get_NX();
            int size_y = grid.Get_NY();
            Point2D.Float coord[][] = grid.Get_Im_Coords();
            map.Patch_Map(coord, size_x, size_y);
            if (mask_exist){
                //mask.ResetFlag();
                mask = null;
            }
            mask_exist = false;
            mask_shown = false;
            ShowGridInfo(grid.Get_StrType());
            String str = InfoField.getText() + "; Mask doesn't exist, current elevation threshold is " + map.GetThreshold();
            InfoField.setText(str);
            //SaveAllMenuItem.setEnabled(true);
            MapEditCheckBox.setEnabled(true);
            MapEditCheckBox.setSelected(true);
            TButtonMap.setEnabled(true);
            MapEditCheckBoxActionPerformed(null);
            RestoreMapMenuItem.setEnabled(true);
        }  
    }*/
    private void GridShownCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GridShownCheckBoxActionPerformed
        grid_shown = GridShownCheckBox.getState();
        RefreshDisplay();
    }//GEN-LAST:event_GridShownCheckBoxActionPerformed

    private void ZoomInMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ZoomInMenuItemActionPerformed
        if(zoom_level <= ZOOM_MAX-scale_factor*ZOOM_INIT){
            zoom_level = zoom_level + scale_factor*ZOOM_INIT;
            ZoomOutMenuItem.setEnabled(true);
            ButtonZoomOut.setEnabled(true);  
            if(zoom_level >= ZOOM_MAX-scale_factor*ZOOM_INIT){
                ZoomInMenuItem.setEnabled(false);
                ButtonZoomIn.setEnabled(false); 
            }
        }
        RefreshDisplay();
    }//GEN-LAST:event_ZoomInMenuItemActionPerformed

    private void ConsGridMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ConsGridMenuItemActionPerformed
        Point NumNodes;
        //RefreshDisplay();
        if(map_exist){
            cgd.setNodes(map.getNLON(),map.getNLAT());
            cgd.show();
            NumNodes = cgd.getReturnStatus();
            if (NumNodes != null){
                if((NumNodes.getX()>map.GetWidth())||(NumNodes.getY()>map.GetHeight())){
                        JOptionPane.showMessageDialog(this,
                                "Too high resolution for loaded elevation data",
                                "Illegal size error",
                                 JOptionPane.ERROR_MESSAGE);
                }else {                
                    CreateRegularGrid(NumNodes);
                }
            }           
        }
        ShowGridInfo(0);
        RefreshDisplay();
    }//GEN-LAST:event_ConsGridMenuItemActionPerformed

    private void CreateRegularGrid(Point NumNodes){
        float left_x = map.getBounds()[0];
        if (left_x < 0)left_x = left_x + 360;
        grid = new Grid(map.GetWidth()-1, map.GetHeight()-1,(int)Math.round(NumNodes.getX()),(int)Math.round(NumNodes.getY()),
                        left_x, map.getBounds()[2], map.getNLON(), map.getNLAT());
        grid_exist = true;
        if(mask_exist){
            mask = null;
            mask_exist = false;
        }
        if (itopo_exist){
            itopo_exist = false;
            itopo_shown = false;
        }
    }
           
    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
        System.exit(0);
    }//GEN-LAST:event_exitMenuItemActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]){
        MaskBuilder maskbuilder = new MaskBuilder();
        maskbuilder.show();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem AnyLayerMenuItem;
    private javax.swing.JCheckBoxMenuItem BWCheckBoxMenuItem;
    private javax.swing.JMenuItem BuildMaskMenuItem;
    private javax.swing.JButton ButtonGrid;
    private javax.swing.JButton ButtonMapLoad;
    private javax.swing.JButton ButtonMaskBuild;
    private javax.swing.JButton ButtonPrint;
    private javax.swing.JButton ButtonZoomIn;
    private javax.swing.JButton ButtonZoomOut;
    private javax.swing.JMenu ChooseRuleMenu;
    private javax.swing.JCheckBoxMenuItem ColCheckBoxMenuItem;
    private javax.swing.JMenuItem ConsGridMenuItem;
    private javax.swing.JCheckBoxMenuItem ContourShownCheckBox;
    private javax.swing.JTextField CoordinateField;
    private javax.swing.JMenu DataMenu;
    private javax.swing.JToggleButton EditMaskButton;
    private javax.swing.JMenuItem EssicStretchMenuItem;
    private javax.swing.JRadioButtonMenuItem FALSE_RadioButton;
    private javax.swing.JMenuItem FilterMenuItem;
    private javax.swing.JMenuItem FisheryItem;
    private javax.swing.JCheckBoxMenuItem GridShownCheckBox;
    private javax.swing.JPanel HintPanel;
    private javax.swing.JLabel ImageLabel;
    private javax.swing.JTextField InfoField;
    private javax.swing.JMenuItem InterpolationItem;
    private javax.swing.JMenuItem LoadContourMenuItem;
    private javax.swing.JMenuItem LoadMapMenuItem;
    private javax.swing.JMenuItem LoadMaskMenuItem;
    private javax.swing.JMenuItem LoadSavedDataMenuItem;
    private javax.swing.JCheckBoxMenuItem MaskModeCheckBox;
    private javax.swing.JCheckBoxMenuItem MaskShownCheckBox;
    private javax.swing.JScrollPane MyScrollPane;
    private javax.swing.JMenu PaletteMenu;
    private javax.swing.JMenuItem PrintAllMenuItem;
    private javax.swing.JMenuItem ResetThresholdMenu;
    private javax.swing.JMenuItem SaveAllMenuItem;
    private javax.swing.JMenuItem SaveGridMenuItem;
    private javax.swing.JMenuItem SaveMaskMenuItem;
    private javax.swing.JMenuItem SaveTopoMenuItem;
    private javax.swing.JMenuItem Stretch2DMenuItem;
    private javax.swing.JMenuItem StretchCombMenuItem;
    private javax.swing.JToggleButton TButtonMask;
    private javax.swing.JToggleButton TButtonScale;
    private javax.swing.JRadioButtonMenuItem TRUE_RadioButton;
    private javax.swing.JMenuItem TopographyMenuItem;
    private javax.swing.JCheckBoxMenuItem TopographyShownCheckBox;
    private javax.swing.JMenu ViewMenu;
    private javax.swing.JRadioButtonMenuItem XOR_RadioButton;
    private javax.swing.JMenuItem ZoomFactorMenuItem;
    private javax.swing.JMenuItem ZoomInMenuItem;
    private javax.swing.JMenuItem ZoomOutMenuItem;
    private javax.swing.JMenuItem aboutMenuItem;
    private javax.swing.JMenuItem contentsMenuItem;
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenu gridMenu;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JSeparator jSeparator5;
    private javax.swing.JSeparator jSeparator6;
    private javax.swing.JSeparator jSeparator7;
    private javax.swing.JSeparator jSeparator8;
    private javax.swing.JMenu mapMenu;
    private javax.swing.JMenu maskMenu;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem restoreMenuItem;
    // End of variables declaration//GEN-END:variables
    
}

